package com.finchina.myview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

/**
 * @author 黄赛赛
 * @version V1.0
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: ${todo}()
 * @date 2015/9/9 13:35
 */
public class CustomVolumControlBar extends View  implements View.OnTouchListener{

    /**
     * 第一圈的颜色
     */
    private int mFirstColor;

    /**
     * 第二圈的颜色
     */
    private int mSecondColor;
    /**
     * 圈的宽度
     */
    private int mCircleWidth;
    /**
     * 画笔
     */
    private Paint mPaint;
    /**
     * 当前进度
     */
    private int mCurrentCount = 3;

    /**
     * 中间的图片
     */
    private Bitmap mImage;
    /**
     * 每个块块间的间隙
     */
    private int mSplitSize;
    /**
     * 个数
     */
    private int mCount;

    private Rect mRect;

    public CustomVolumControlBar(Context context) {
        this(context, null);
    }

    public CustomVolumControlBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomVolumControlBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomVolumControlBar, defStyleAttr, 0);
        int n = a.getIndexCount();

        mFirstColor = a.getColor(R.styleable.CustomVolumControlBar_VolumfirstColor, Color.GREEN);
        mSecondColor = a.getColor(R.styleable.CustomVolumControlBar_VolumsecondColor, Color.CYAN);
        mImage = BitmapFactory.decodeResource(getResources(), a.getResourceId(R.styleable.CustomVolumControlBar_Volumbg, R.mipmap.ic_launcher));
        mCircleWidth = a.getDimensionPixelSize(R.styleable.CustomVolumControlBar_VolumcircleWidth, (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_PX, 5, getResources().getDisplayMetrics()));
        mCount = a.getInt(R.styleable.CustomVolumControlBar_VolumdotCount, 10);// 默认20
        mSplitSize = a.getInt(R.styleable.CustomVolumControlBar_VolumsplitSize, 10);
        a.recycle();
        mPaint = new Paint();
        mRect = new Rect();
        mPaint.setAntiAlias(true);

        gestureDetector = new GestureDetector(mOnGestureListener);

        setOnTouchListener(this);
    }
    private GestureDetector gestureDetector;

    float startX, endX;

    GestureDetector.OnGestureListener mOnGestureListener = new GestureDetector.OnGestureListener() {
        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        @Override
        public void onShowPress(MotionEvent e) {

        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return false;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            return false;
        }

        @Override
        public void onLongPress(MotionEvent e) {

        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            Log.e("onFling", "velocityX = "+ velocityX + "   velocityY = "+velocityY);
            if (velocityY > 0){
               minus();
           }else{
               add();
           }
            return true;
        }
    };

//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        switch (event.getAction()) {
//            case MotionEvent.ACTION_DOWN:
//                startX = event.getX();
//                break;
//            case MotionEvent.ACTION_UP:
//                endX = event.getX();
//
//                if (startX > endX && startX - endX > 20) {
//                    add();
//                } else if (endX > startX && endX - startX > 20) {
//                    minus();
//                }
//
//                break;
//        }
//        return true;
//    }

    @Override
    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
        canvas.drawColor(Color.YELLOW);
        drawCircle(canvas);
    }

    void drawCircle(Canvas canvas) {

        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mCircleWidth);
        mPaint.setStrokeCap(Paint.Cap.ROUND);

        int centerX = getMeasuredWidth() / 2;
        int centerY = getMeasuredHeight() / 2;
        int min_width = Math.min(centerX, centerX);
//        int radius = min_width / 2 - mCircleWidth;
        int radius = min_width - mCircleWidth;

        RectF oval = new RectF();
        oval.left = centerX - radius;
        oval.right = centerX + radius;
        oval.top = centerY - radius;
        oval.bottom = centerY + radius;

//        mPaint.setColor(Color.RED);
//        canvas.drawRect( oval.left,oval.top,  oval.right, oval.bottom, mPaint);
        int item = (int) ((360.0 - mCount * mSplitSize) / mCount);
        mPaint.setColor(Color.RED);
        for (int i = 0; i < mCount; i++) {
            int start = (item + mSplitSize) * i;
            canvas.drawArc(oval, start -90, item, false, mPaint);
        }

        mPaint.setColor(Color.GREEN);

        for (int i = 0; i < mCurrentCount; i++) {
            int start = (item + mSplitSize) * i;
            canvas.drawArc(oval, start -90, item, false, mPaint);
        }


        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawBitmap(mImage, centerX- mImage.getWidth()/2, centerY - mImage.getHeight()/2, mPaint);
    }


    void add() {
        if (mCurrentCount < mCount) {
            mCurrentCount++;
            invalidate();
        }

    }

    void minus() {
        if (mCurrentCount > 0) {
            mCurrentCount--;
            invalidate();
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }
}


/*<annotion.example.hss_fst.myapplication.CustomVolumControlBar
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:dotCount="10"
        app:splitSize="10"
        app:circleWidth="5dp"/>*/
